package com.yequan.mybatisframework.sqlsession;

import com.yequan.mybatisframework.config.Configuration;
import com.yequan.mybatisframework.config.MappedStatement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 缓存执行器
 * 
 * @author 灭霸詹
 *
 */
public class CachingExecutor implements Executor {

	private Map<String, List<Object>> oneLevelCache = new HashMap<>();

	private BaseExecutor delegate = new BaseExecutor();

	@Override
	public <T> List<T> query(MappedStatement mappedStatement, Configuration configuration, Object paramObj) {
		String sql = mappedStatement.getSqlSource().getBoundSql().getSql();
		if (oneLevelCache.containsKey(sql)) {
			return (List<T>) oneLevelCache.get(sql);
		}
		// 如果缓存执行器中没有对应的结果，那么就委托给基本执行器去执行CRUD操作
		List<Object> result = delegate.query(mappedStatement, configuration, paramObj);
		oneLevelCache.put(sql, result);
		return (List<T>) result;
	}

}
